#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查缺失的图片文件
"""

import os
import re

def extract_image_references(directory="."):
    """提取代码中引用的图片文件名"""
    referenced_images = set()
    
    # 搜索JS文件中的图片引用
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.js'):
                file_path = os.path.join(root, file)
                try:
                    with open(file_path, 'r', encoding='utf-8') as f:
                        content = f.read()
                    
                    # 匹配 /images/xxx.png 的模式
                    pattern = r'/images/([^"]+\.png)'
                    matches = re.findall(pattern, content)
                    referenced_images.update(matches)
                    
                except Exception as e:
                    print(f"读取文件 {file_path} 时出错: {e}")
    
    return referenced_images

def check_missing_images(referenced_images, images_dir="images"):
    """检查哪些引用的图片文件缺失"""
    existing_images = set()
    if os.path.exists(images_dir):
        for file in os.listdir(images_dir):
            if file.endswith('.png'):
                existing_images.add(file)
    
    missing_images = referenced_images - existing_images
    return missing_images, existing_images

def main():
    print("正在检查缺失的图片文件...")
    
    # 提取代码中引用的图片
    referenced_images = extract_image_references()
    print(f"代码中引用了 {len(referenced_images)} 个图片文件")
    
    # 检查缺失的图片
    missing_images, existing_images = check_missing_images(referenced_images)
    
    print(f"\n当前存在的图片文件: {len(existing_images)} 个")
    print(f"缺失的图片文件: {len(missing_images)} 个")
    
    if missing_images:
        print("\n缺失的图片文件:")
        for img in sorted(missing_images):
            print(f"  - {img}")
        
        print(f"\n需要恢复 {len(missing_images)} 个图片文件")
    else:
        print("\n✅ 所有引用的图片文件都存在！")

if __name__ == '__main__':
    main() 